<?php

vendor('Upcoming');

class Upcoming extends AppModel {
	
	var $name = 'Upcoming';
	var $useTable = false;  
	
	// You should set these manually
	// if not storing them in a db
	var $venue_id = null;
	var $category_id = null;
	var $token = null;
	var $API_KEY = '';
	var $UPCOMING_DEBUG = false;
	
	function __construct() {
		parent::__construct();
		$this->options = array(
		        'DEBUG' => $this->UPCOMING_DEBUG, // Flip to true to see what the class is doing.
		        'api_key' => $this->API_KEY, // Place your API Key here.
		        'endpoint' => 'http://upcoming.yahoo.com/services/rest/',
		        'auth_endpoint' => 'http://upcoming.yahoo.com/services/auth/',
				'cache_dir' => ROOT . DS . APP_DIR . DS . 'tmp/cache/services/upcoming/'
		        );
		$this->errorArray = array();
		
		
	}
		
	function findAll ( $conditions )
	{
		
		$search = array(
		            'method' => 'event.search',
		            'http_method' => 'GET',
		            'params' => $conditions
					);
		
		return $this->__upcCallMethod($search);
		
	}
	
	function add ($params)
	{

		$params['token'] = $this->token;
		$params['venue_id'] = $this->venue_id;
		$params['category_id'] = $this->category_id;
		
		$query = 
					array(
		                 'method' => 'event.add',
		                 'http_method' => 'POST',
		                 'params' => $params,
		                 'nodeType' => 'event',
		                 'attributes' => array(
		                     'id',
		                     'name'
		                     )
		              
					);
		
			
		return $this->__upcCallMethod ( $query );
					
	}
	
	function edit ( $params )
	{

		$params['token'] = $this->token;
		$params['venue_id'] = $this->venue_id;
		$params['category_id'] = $this->category_id;
		
		$query = 
					array(
		                 'method' => 'event.edit',
		                 'http_method' => 'POST',
		                 'params' => $params,
		                 'nodeType' => 'event',
		                 'attributes' => array(
		                     'id',
		                     'name'
		                     )
		              
					);
		
			
		return $this->__upcCallMethod ( $query );
	}
	
	
	
	function read ( $id='' )
	{
		$event = array(
		        array(
		            'method' => 'event.getInfo',
		            'http_method' => 'GET',
		            'params' => array(
		                'event_id' => $id
		                ),
		            'nodeType' => 'event',
		            'attributes' => array(
		                'id',
		                'name'
		                )
		            )
					);
					
					
	}
	
	function __upcCallMethod ( $query )
	{
		$this->upc =& new Services_Upcoming($this->options, $this->errorArray);
		$xml = $this->upc->callMethod($query['method'], $query['params'], $query['http_method']);
		return $xml;
	}
	
	//deprecated, split into controller and views
	function __getToken ()
	{
		
		$this->upc =& new Services_Upcoming($this->options, $this->errorArray);
		if (isset($_POST['frob'])) {
		    $frob = $_POST['frob'];
		}
		
		if ($this->SAVED_TOKEN == '' && isset($_POST['token'])) {
		    $this->token = $_POST['token'];
		} else {
		    if ($this->SAVED_TOKEN != '') {
		        $this->token = $this->SAVED_TOKEN;
		    }
		}
		
		if (!isset($this->token)) {
		    if (!isset($this->frob)) {
				$auth_url = $this->upc->getAuthUrl();
		        print '
		            <h1>Services_Upcoming Test Setup</h1>

		            <b>Important!</b>: Make sure to navigate the tests using form buttons only. <br />
		            The token you get for interaction here only lasts while you\'re in a single visit. <br />
		            (Hint: You can edit the page and save the token to make sure it lasts.)
		            <br />
		            In order to run some of the data-modifying test cases, you need to <br />
		            authorize this example via Upcoming.org. Here\'s how you do it: 
		            <br />
		            <br />
		            If you did NOT add a callback URL when you signed up for an API key, then <br />
		            <a href="'.$auth_url.'">Please visit here, and come back and paste in your token.</a>
		            <br />
		            <br />
		            If you did (or would like to), you\'ll need to clear it temporarily, or just <br />
		            set up this page\'s URL as your<br />
		            callback URL temporarily.
		            <br />
		            <br />
		            <a href="'.$KEYGEN_URL.'">Go here to make the necessary changes.</a>
		            <br />
		            <br />
		            Once you\'re back, put the code you received in here:
		            <form method="post">
		            <input type="text" name="frob" value="'.htmlspecialchars($frob).'"/>
		            <input type="submit" name="submit" value="Submit" />
		            </form>
		            ';
			} else {
		        // Get a token from the frob.
				
		        $xml = $this->upc->callMethod('auth.getToken', array('frob' => '$frob'));
		        $this->token = (string)$xml['token']['token'];
			}
		}
	}
	
} 

?>